﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VisualAgile.DTO;
using VisualAgile.Model;
using VisualAgile.DTO.Mappers;
using VisualAgile.Controllers;
using System.Collections;

namespace ProjectRouting.Controllers
{
    public class FeatureController : BaseController
    {
        // POST: /Feature/Update

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Update(FeatureDTO Json, Boolean Update)
        {
            var rdto = new ResultDTO();
            Feature feature = FeatureDTOMapper.MapFromDTO(Json);
            Release release = ManagerFactory.ReleaseManager.Get(new Guid(feature.ReleaseID));
            
            if (!Update)
            {
                feature.Target = release.PeriodTo;                
                feature.FeatureID = Guid.Empty;
                feature.Name = "New Feature";
                feature.CreateTime = DateTime.Now;
            }

            try
            {                
                if (DateTime.Compare(feature.Target, release.PeriodTo) > 0)
                    feature.Target = release.PeriodTo;
                feature = ManagerFactory.FeatureManager.Update(feature);
                rdto.Result = true;
                rdto.Message = "Success";
                rdto.Data = FeatureDTOMapper.MapToDTO(feature).ToExtJsJson();
            }
            catch (Exception e)
            {
                rdto.Message = e.Message;
                rdto.Result = false;
            }
          
            return this.Json(rdto);
        }

        // POST: /Feature/Delete/GUID

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Delete(Guid Guid)
        {
            var rdto = new ResultDTO();
            try
            {
                ManagerFactory.FeatureManager.Delete(Guid);
                rdto.Message = "Success";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = ex.Message;
                rdto.Result = false;
            }
            return this.Json(rdto);
        }

    }
}
